package com.atguigu.edu.realtime.utils;

import com.atguigu.edu.realtime.beans.TrafficPageViewBean;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class BeanList {
    public static List<Object> convertObjectToCollection(Object obj) throws IllegalAccessException {
        // 获取对象的类
        Class<?> clazz = obj.getClass();

        // 获取对象的所有属性
        Field[] fields = clazz.getDeclaredFields();

        // 创建一个集合对象
        List<Object> collection = new ArrayList<>();

        // 遍历属性列表并将属性值添加到集合中
        for (Field field : fields) {
            field.setAccessible(true); // 设置私有属性可访问
            String fieldName = field.getName(); // 获取属性名
            if (!fieldName.equals("ts") ){
                Object value = field.get(obj); // 获取属性值
                if (value != null){
                    collection.add(value); // 添加到集合中
                }
            }
        }

        return collection;
    }

    public static void main(String[] args) throws IllegalAccessException {
        List<Object> objects = convertObjectToCollection(new TrafficPageViewBean());
        System.out.println("objects = " + objects);
    }
}
